<template>
  <div class="custom-table-list-wrapper">
    <div class="search" :class="{ active: toggleSearchStatus }">
      <div class="search-left" ref="searchFromRef">
        <el-form class="search-form" inline>
          <el-form-item label="检查类型">
            <el-select
              class="search-select"
              v-model="searchParams.typeId"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dict['inspection_inspection_type']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="检查园区">
            <el-select
              class="search-select"
              v-model="searchParams.areaId"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of parkOptions"
                :key="index"
                :label="item.name"
                :value="item.pkid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="任务状态">
            <el-select
              class="search-select"
              v-model="searchParams.status"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of parkOptions"
                :key="index"
                :label="item.name"
                :value="item.pkid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              class="search-input"
              v-model="searchParams.keyword"
              placeholder="请输入任务名称、巡检人员姓名"
              :suffix-icon="Search"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="任务时间">
            <el-date-picker
              v-model="searchParams.time"
              type="daterange"
              value-format="YYYY-MM-DD"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
        </el-form>
        <a
          class="search-left-toggle"
          v-if="showToggle"
          @click="handleToggleSearch"
        >
          <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
          {{ toggleSearchStatus ? '收起' : '展开' }}
        </a>
      </div>
      <div class="search-right">
        <el-button type="primary" @click="initTableList(1)">
          <svg-icon name="search" size="14px" />
          搜索
        </el-button>
        <el-button @click="handleReset()">
          <svg-icon name="reset" size="17px" />
          重置
        </el-button>
      </div>
    </div>
    <div class="content">
      <div class="content-table">
        <el-table class="custom-table" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" />
          <el-table-column prop="name" label="巡检任务名称" />
          <el-table-column prop="time" label="任务时间">
            <template #default="scope">
              {{ moment(scope.row.beginTime).format('YYYY-MM-DD') }} -
              {{ moment(scope.row.endTime).format('YYYY-MM-DD') }}
            </template>
          </el-table-column>
          <el-table-column label="检查类型">
            <template #default="scope">
              <span>{{
                getDictValue('inspection_inspection_type', scope.row.typeId)
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="areaName" label="巡检园区" />
          <el-table-column prop="completedTime" label="完成时间"
            ><template #default="scope">
              {{
                scope.row.completedTime
                  ? moment(scope.row.completedTime).format('YYYY-MM-DD')
                  : ''
              }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="任务状态">
            <template #default="scope">
              <el-tag :type="getDictStyle('task_status', scope.row.status)">{{
                getDictValue('task_status', scope.row.status)
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="160px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="
                  handleDetail(
                    { pkid: scope.row.pkid },
                    'taskManageDetail',
                    true,
                  )
                "
                >详情
              </el-button>
              <el-button
                v-if="scope.row.status === 1"
                link
                type="primary"
                @click="handleDetail(scope.row, 'taskManageDetail', true)"
                >结果确认
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="content-page">
        <el-pagination
          class="custom-pagination"
          :current-page="paginationParams.pageIndex"
          :page-size="paginationParams.pageSize"
          background
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="initTableList"
        />
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import moment from 'moment'
import { getTaskList } from '@/api/manager/inspectionManage/taskManage.js'
import { parkPageList } from '@/api/manager/dataManage/park'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDict } from '@/hooks/useDict'
const { dict, getDictStyle, getDictValue } = useDict([
  'inspection_inspection_type',
  'inspection_status',
])

const searchParams = ref({
  name: '',
  startTime: '',
  person: '',
  status: '',
})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, getTaskList)

const handleConfirmResult = (val) => {
  console.log('巡检结果确认', val)
}

// 获取园区列表
const parkOptions = ref([])
const initParkOptions = async () => {
  const res = await parkPageList({
    pageIndex: 1,
    pageSize: 9999,
    enable: '1',
  })
  parkOptions.value = res.data.list
}

onMounted(() => {
  initParkOptions()
  initTableList(1)
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
</style>
